package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlRfwork;
import com.ruoyi.sl.service.ISlRfworkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报工单Controller
 * 
 * @author admin
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/sl/rfwork")
public class SlRfworkController extends BaseController
{
    @Autowired
    private ISlRfworkService slRfworkService;

    /**
     * 查询报工单列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlRfwork slRfwork)
    {
        startPage();

        //如果不是管理员则只查询登录用户自己的报工
        LoginUser loginUser = getLoginUser();
        if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin()){
            slRfwork.setRfworkUser(getLoginUser().getUsername());
        }

        List<SlRfwork> list = slRfworkService.selectSlRfworkList(slRfwork);
        return getDataTable(list);
    }

    /**
     * 导出报工单列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:export')")
    @Log(title = "报工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlRfwork slRfwork)
    {
        List<SlRfwork> list = slRfworkService.selectSlRfworkList(slRfwork);
        ExcelUtil<SlRfwork> util = new ExcelUtil<SlRfwork>(SlRfwork.class);
        util.exportExcel(response, list, "报工单数据");
    }

    /**
     * 获取报工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:query')")
    @GetMapping(value = "/{rfworkId}")
    public AjaxResult getInfo(@PathVariable("rfworkId") Long rfworkId)
    {
        return success(slRfworkService.selectSlRfworkByRfworkId(rfworkId));
    }

    /**
     * 新增报工单
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:add')")
    @Log(title = "报工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlRfwork slRfwork)
    {
        return toAjax(slRfworkService.insertSlRfwork(slRfwork));
    }

    /**
     * 修改报工单
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:edit')")
    @Log(title = "报工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlRfwork slRfwork)
    {
        return toAjax(slRfworkService.updateSlRfwork(slRfwork));
    }

    /**
     * 删除报工单
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:remove')")
    @Log(title = "报工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rfworkIds}")
    public AjaxResult remove(@PathVariable Long[] rfworkIds)
    {
        return toAjax(slRfworkService.deleteSlRfworkByRfworkIds(rfworkIds));
    }
}
